package ch3;

/**
 * Created by wyao on 17-3-6.
 * 用Clone()方式创建对象
 */
public class C3_1 {
    private static Credit BaseCredit = new Credit();

    public static Credit getNewCredit() {
        return new Credit();
    }

    public static Credit getNewCredit2() throws CloneNotSupportedException {
        return (Credit) BaseCredit.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            Credit cc = C3_1.getNewCredit();
            cc.setSize(i);
        }
        System.out.println(System.currentTimeMillis() - start);

        start = System.currentTimeMillis();
        for (int i = 0; i < 1000000000; i++) {
            Credit cc = C3_1.getNewCredit2();
            cc.setSize(i);
        }
        System.out.println(System.currentTimeMillis() - start);
    }
}

class Credit implements Cloneable {
    int size;

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (Credit) super.clone();
    }
}
